﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Threading;
using System.Windows;
using Cad;
using System.Windows.Forms;

namespace ServiceHoloXia
{

    static class Program
    {
        /// <summary>
        /// Point d'entrée principal de l'application.
        /// </summary>
        static void Main()
        {
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[]
            {
                new Service1()
            };
            ServiceBase.Run(ServicesToRun);
        }
    }

    public class Ordinateur
    {
        private static readonly Ordinateur instance = new Ordinateur();
        public int ram = 0;
        public int cpu = 0;
        public DateTime now;

        public static Ordinateur Instance
        {
            get
            {
                return instance;
            }

        }

        private Ordinateur() {}

        public int getRAM()
        {
            PerformanceCounter ramCounter;
            ramCounter = new PerformanceCounter("Memory", "Available MBytes");
            ram = (int)ramCounter.NextValue();
            Console.WriteLine(ram);
            return ram;
        }

        public int getCPU()
        {
            PerformanceCounter cpuCounter;
            
            cpuCounter = new PerformanceCounter();

            cpuCounter.CategoryName = "Processor";
            cpuCounter.CounterName = "% Processor Time";
            cpuCounter.InstanceName = "_Total";
            
            Console.WriteLine(cpuCounter.NextValue() + "%"); //getCurrentCpuUsage
            Thread.Sleep(1000);
            

            cpu = (int)cpuCounter.NextValue();
            Console.WriteLine(cpu);

            return cpu;
        }

        public DateTime getDate()
        {
            now = DateTime.Now;

            return now;
        }
    }

    public class IterationThread
    {

        private static readonly IterationThread instanceIterationThread = new IterationThread();

        public static IterationThread InstanceIterationThread
        {
            get
            {
                return instanceIterationThread;
            }

        }

        public IterationThread()
        {
            this.Timer = 120;
        }

        private int timer;
        
        public int Timer
        {
            get
            {
                return timer/1000;
            }

            set
            {
                timer = value*1000;
            }
        }
        private void eXec()
        {
            Cad.CAD proc = new Cad.CAD();
            proc.StockedProcedureExec(Ordinateur.Instance.getCPU(), Ordinateur.Instance.getRAM(), Ordinateur.Instance.getDate());
            Autoriser auto = new Autoriser();
            auto.verifier();
            proc.close();
        }


        public void caller()
        {
            while (true)
            {
                eXec();

                //2min time out
                Thread.Sleep(timer);
            }
        }
            
    }

    public class Verification
    {
        int cpuUtil;
        int RamUtil;
        public Verification()
        {
            
        }

        public int CpuUtil
        {
            get
            {
                return cpuUtil;
            }

            set
            {
                cpuUtil = 80;
            }
        }

        public int RamUtil1
        {
            get
            {
                return RamUtil;
            }

            set
            {
                RamUtil = 1024;
            }
        }

        public bool IsStartable(int getCpu, int getRam)
        {
            if (getCpu < CpuUtil && getRam < RamUtil1)
            {
                return true;
            }
            return false;
        }
    }

    public class Autoriser
    {
        
        private bool startable;

        public bool Startable
        {
            get
            {
                return startable;
            }

            set
            {
                startable = value;
            }
        }


        public bool IsStartabled()
        {
            if (!startable)
            {
                ///// lancer MESSAGE ERREUR, dans un thread!!! <3 poney
            }
            return startable;

        }
        public Autoriser()
        {
            
        }

        public void verifier()
        {
        
            Verification verif = new Verification();
            if (verif.IsStartable(Ordinateur.Instance.getCPU(), Ordinateur.Instance.getRAM()))
            {
                AutorisationHoloXia();
            }
            else RefuserHoloXia();
        }
        public bool AutorisationHoloXia()
        {
            return true;
        }

        public bool RefuserHoloXia()
        {
            return false;
        }
    }
}
